<?php
namespace app\index\controller;
use think\Controller;
use app\common\model\Teacher;       // 教师模型
use think\Request;			// 引用Request

class TeacherController extends Controller
{
    public function index()
    {
    	try{
			// $Teacher 首写字母大写，说明它是一个对象，更确切一些说明这是基于Teacher这个模型被我们手工实例化得到的，如果存在teacher数据表，它将对应teacher数据表。
	        $Teacher = new Teacher; 

	        // $teachers 以s结尾，表示它是一个数组，数据中的每一项都是一个对象，这个对象基于Teahcer这个模型。
	        $teachers = $Teacher->select();

	        // 向V层传数据
	        $this->assign('teachers', $teachers);

	        // 取回打包后的数据
	        $htmls = $this->fetch();

	        // 将数据返回给用户
	        return $htmls;
    	}catch(\Exception $e){
    		return '系统错误' . $e->getMessage();
    	}
    }

	public function add()
    {
    	try{
    		$htmls = $this->fetch();
    		return $htmls;
    	}catch(\Exception $e){
    		return '系统错误' . $e->getMessage();
    	}
    }

    public function insert()
    {
    	$message = '';
    	try{
	    	// Request::instance()返回了一个对象，调用这个对象的post()方法，得到post数据
	        $postData = Request::instance()->post();
	        //$postData = $this->request->post();

	        // 实例化Teacher空对象
	        $Teacher = new Teacher();
	        
	        // 为对象的属性赋值
	        $Teacher->name = $postData['name'];
	        $Teacher->username = $postData['username'];
	        $Teacher->sex = $postData['sex'];
	        $Teacher->email = $postData['email'];

	        // 新增对象至数据表
	        $result = $Teacher->validate(true)->save();

	        // 执行对象的插入数据操作
	        //$Teacher->save();
	        if($result === false){
	        	$message = '新增失败:' . $Teacher->getError();
	        }else{
	        	return $this->success('用户' . $Teacher->name . '新增成功。新增ID为:' . $Teacher->id, url('index'));
	        }
	    }catch(\think\Exception\HttpResponseException $e){
	    	throw $e;
	    }catch(\Exception $e){
	    	return $e->getMessage();
	    }
        return $this->error($message);
    }

    public function edit()
    {
    	//获取传入的ID
    	$id = input('id/d');
    	//在Teacher表模型中获取当前ID对应的记录
    	if($Teacher = Teacher::get($id)){
	    	//将数据传给V层
	    	$this->assign('Teacher', $Teacher);
	    	//获取封装好的V层内容
	    	$htmls = $this->fetch();
	    	//将封装好的V层内容返回给用户
	    	return $htmls;
	    }
	    return "系统没有找到ID为{$id}的记录";
    }

    public function update()
    {
    	try{
	    	//接收数据
	    	$id = input('id/d');
	    	$name = input('name');
	    	$username = input('username');
	    	$sex = input('sex/d');
	    	$email = input('email');

	    	$message = '更新成功';
	    	//将数据存入Teacher表
	    	$Teacher = Teacher::get($id);
	    	if(is_object($Teacher)){
	    		$Teacher->name = $name;
	    		$Teacher->username = $username;
	    		$Teacher->sex = $sex;
	    		$Teacher->email = $email;
				//更新
	    		if(false === $Teacher->validate(true)->save()){
		    		$message = '更新失败：' . $Teacher->getError();
		    	}
	    	}else{
	    		throw new \Exception('所更新的记录不存在', 1);
	    	}
    	}catch(\Exception $e){
    		$message = $e->getMessage();
    	}
    	return $message;
    }

    public function delete()
    {
    	try{
    		//实例化请求类
    		$Request = Request::instance();
	    	//$id = Request::instance()->get('id');
	    	$id = input('id/d');
	    	//$id = Request::instance()->param('id/d');
	    	if($id == 0){
	    		//return $this->error('未获取到正确的ID信息');
	    		throw new \Exception("未正确获取ID信息", 1);
	    	}

	    	$Teacher = Teacher::get($id);//获取要删除的对象
	    	if(is_null($Teacher)){
	    		//return $this->error("不存在ID为{$id}的教师，删除失败");
	    		throw new \Exception('不存在ID为'.$id.'的教师，删除失败', 1);
	    	}
	    	//直接删除相关关键字数据
	    	// if($count = Teacher::destroy($id)){
	    	// 	return "成功删除{$count}条数据";
	    	// }
	    	// return '删除失败!';

	    	//删除对象
	    	if(!$Teacher->delete()){
	    		$message = '删除失败：' . $Teacher->getError();
	    	}
	    	//成功，跳转
	    	return $this->success('删除成功', $Request->header('referer'));
	    } catch (\think\Exception\HttpResponseException $e) {
            throw $e;
        // 获取到正常的异常时，输出异常
    	}catch(\Exception $e){
    		var_dump($e);
    		return $e->getMessage();
    	}
    	return $this->error($message);
    }

    public function test()
    {
        try {
            throw new \Exception("Error Processing Request", 1);
            return $this->error("系统发生错误");

        // 获取到ThinkPHP的内置异常时，直接向上抛出，交给ThinkPHP处理
        } catch (\think\Exception\HttpResponseException $e) {
            throw $e;

        // 获取到正常的异常时，输出异常
        } catch (\Exception $e) {
            return $e->getMessage();
        } 
    }
}